﻿// DigitalDesign
// Project: InformationBoard
// Developer: Bobylev Dmitry
// Created: 13.11.2011
// Last change: 28.11.2011
// Description: methods of screenshots page.

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using GUIAdmin.AdminServiceReference;
using GUIAdmin.CallbackServiceReference;

namespace GUIAdmin.Views
{
    public partial class Screenshots : Page
    {
        private AdminServiceClient adminClient = new AdminServiceClient();
        private CallbackServiceClient callbackClient = new CallbackServiceClient();

        public Screenshots()
        {
            InitializeComponent();

            adminClient.GetListOfClientsCompleted +=
                new System.EventHandler<GetListOfClientsCompletedEventArgs>
                    (adminClient_GetListOfClientsCompleted);
            callbackClient.GetLinkOnScreenShotCompleted +=
                new EventHandler<GetLinkOnScreenShotCompletedEventArgs>
                    (callbackClient_GetLinkOnScreenShotCompleted);
            callbackClient.GetListOfScreenShotsOnClientCompleted +=
                new EventHandler<GetListOfScreenShotsOnClientCompletedEventArgs>
                    (callbackClient_GetListOfScreenShotsOnClientCompleted);
            callbackClient.DeleteScreenShotCompleted +=
                new EventHandler<AsyncCompletedEventArgs>
                    (callbackClient_DeleteScreenShotCompleted);
            callbackClient.DeleteScreenShotsCompleted +=
                new EventHandler<AsyncCompletedEventArgs>
                    (callbackClient_DeleteScreenShotCompleted);
        }

        // Выполняется, когда пользователь переходит на эту страницу.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            Image.Visibility = Visibility.Collapsed;
            adminClient.GetListOfClientsAsync();
        }


        #region ServiceReference EventHandler
        private void callbackClient_DeleteScreenShotCompleted(object sender, AsyncCompletedEventArgs e)
        {
            callbackClient.GetListOfScreenShotsOnClientAsync(((ClientNotEF)ClientList.SelectedItem).Id);
        }

        private void adminClient_GetListOfClientsCompleted(object sender, GetListOfClientsCompletedEventArgs e)
        {
            if (e.Result != null)
                ClientList.ItemsSource = e.Result;
        }

        private void callbackClient_GetLinkOnScreenShotCompleted
            (object sender, GetLinkOnScreenShotCompletedEventArgs e)
        {
            if (e.Result != null)
                ScreenShot.Source = new BitmapImage(new Uri(e.Result));
            else MessageBox.Show("Getting a picture is failed.");
        }

        private void callbackClient_GetListOfScreenShotsOnClientCompleted
            (object sender, GetListOfScreenShotsOnClientCompletedEventArgs e)
        {
            if (e.Result.Count > 0)
                   ScreenGrid.ItemsSource = e.Result;
        }

        #endregion

        #region Button Click

        //удаление скриншота
        private void DeleteScreen_Click(object sender, RoutedEventArgs e)
        {
            if (ScreenGrid.SelectedIndex != -1)
                callbackClient.DeleteScreenShotAsync(((ScreenShotWarp)ScreenGrid.SelectedItem).ScreenShotID);
        }

        //удаление всех скриншотов
        private void DeleteAllScreen_Click(object sender, RoutedEventArgs e)
        {
            if (ClientList.SelectedIndex != -1)
                callbackClient.DeleteScreenShotsAsync(((ClientNotEF)ClientList.SelectedItem).Id);
        }

        #endregion

        #region Selection changes

        //показ выбранного скриншота
        private void ScreenGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Image.Visibility = Visibility.Visible;
            if (ScreenGrid.SelectedIndex != -1)
                callbackClient.GetLinkOnScreenShotAsync
                    (((ScreenShotWarp)ScreenGrid.SelectedItem).ScreenShotID);
        }

        //получение списка скриншотов на клиенте
        private void ClientList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ClientList.SelectedIndex != -1)
                callbackClient.GetListOfScreenShotsOnClientAsync
                    (((ClientNotEF)ClientList.SelectedItem).Id);
        }

        #endregion
    }
}
